/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package homomorphism.list;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author liuyu
 */
public abstract class ThirdHomomorphismTheorem<T1 extends Object,T2 extends Object> implements ThirdHomomorphismInterface<T1,T2> {

    public abstract T2 fold(ArrayList<T1> values) ;

    public abstract ArrayList<T1> unfold(T2 value) ;
    
    public T2 fold(Iterator<T1> values) {
        T2 rst = null;
        while (values.hasNext()) {
            final T1 e = values.next();
            if (rst == null) {
                rst = fold(new ArrayList<T1>() {{add(e);};});
            }else{
                ArrayList<T1> tmp = new ArrayList<T1>();
                //tmp.add(unfold(rst));
                tmp.addAll(unfold(rst));
                tmp.add(e);
                rst = fold(tmp);
            }
        }
        return rst;
    }

    public Object GetValue(T2 val) {
        return val;
    }
}
